package com.wyu.servlet;

import com.alibaba.fastjson.JSON;
import com.wyu.pojo.SC;
import com.wyu.service.SCService;
import com.wyu.service.SCServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/SelectScServlet")
public class SelectScServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String page = request.getParameter("page");
        String limit = request.getParameter("limit");
        System.out.println("当前页: "+page+"查询的条数:"+limit);
        //将两个参数提交到service层完成逻辑处理
        SCService scservice = new SCServiceImpl();
        //有个分页的方法
        List<SC> list= scservice.selectByXSpage(page,limit);
        //将返回的数据封装成json数据，因为layui数据表格只识别json数据
        //json数据要遵循一定的规则
        //code---默认0
        //msg---默认空
        //data---得到的list集合
        //count---表格中的总条数

        String listjson = JSON.toJSONString(list);

        //查询数据库有多少条
        int count = scservice.getCount();
        String json ="{\"code\": 0,\"msg\": \"\",\"count\": "+count+",\"data\": "+listjson+"}";

        //控制台输出
        System.out.println(json);
        //将数据输出到浏览器
        response.getWriter().append(json);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }
}
